local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local os = os
local string = string
local tonumber = tonumber
local pairs = pairs

module(...)

_VERSION = '0.1'

function get_route(routeid)
    local json = require "cjson"
    local conf = require "conf"
    local db = conf.get_mysql()
    local res = db:query("SELECT * FROM `route` WHERE id = " .. routeid)
    if not res then
        ngx.print(json.encode({
            err = 2,
            msg = '没有找到对应的航路'
        }))
        ngx.exit(200)
    end
    local res1 = db:query("SELECT id, name, phone, contact_name FROM `spoint` WHERE id = " .. res[1].sid .. " OR id = " .. res[1].eid)
    if not res1 or #res1 < 2 then
        ngx.print(json.encode({
            err = 2,
            msg = '没有找到对应的起降点'
        }))
        ngx.exit(200)
    end

    local airport = {}
    local arr = {res[1].sid, res[1].eid}

    for i,id in pairs(arr) do
        for k,v in pairs(res1) do
            if v.id == id then
                airport[#airport + 1] = v
            end
        end
    end
    
    return {
        route = res[1],
        airport = airport
    }
end

function default(args)
    local json = require "cjson"
    local conf = require "conf"
    local user = conf.parse_token(args, "config")
    if args.action == 'route' then
        local id = tonumber(args.id)
        if not id then
            ngx.print(json.encode({
                err = 1,
                msg = '没有id'
            }))
            ngx.exit(200)
        end
        local db = conf.get_mysql()
        local res = db:query("SELECT *, (SELECT name from `spoint` where id = sid) AS sname, (SELECT name from `spoint` where id = eid) AS ename, (SELECT phone from `spoint` where id = sid) AS sphone, (SELECT phone from `spoint` where id = eid) AS ephone FROM `route` WHERE id = " .. id .. " AND aid in (" ..  table.concat(user.aids, ",") .. ")")
        if not res or not res[1] then
            ngx.print(json.encode({
                err = 2,
                msg = '没有找到对应的航路'
            }))
            ngx.exit(200)
        end
        ngx.print(json.encode({
            err = 0,
            route = res[1]
        }))
    elseif args.action == 'allroute' then
        local db = conf.get_mysql()
        local res = db:query("SELECT *, (SELECT name from `spoint` where id = sid) AS sname, (SELECT name from `spoint` where id = eid) AS ename, (SELECT phone from `spoint` where id = sid) AS sphone, (SELECT phone from `spoint` where id = eid) AS ephone FROM `route` WHERE aid in (" ..  table.concat(user.aids, ",") .. ")")
        if not res then
            ngx.print(json.encode({
                err = 3,
                msg = '没有找到航路'
            }))
            ngx.exit(200)
        end
        local routes = res
        res = db:query("SELECT * FROM `spoint` WHERE aid in (" ..  table.concat(user.aids, ",") .. ")")

        if not res then
            ngx.print(json.encode({
                err = 3,
                msg = '没有找到起降点'
            }))
            ngx.exit(200)
        end
        ngx.print(json.encode({
            err = 0,
            routes = routes,
            airports = res
        }))
    elseif args.action == 'config' then
        ngx.print("var config = { version: \"2.0.2\"};")
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)
